package com.lanou.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.Course;
import com.lanou.entity.CourseCollect;
import com.lanou.entity.MyNotice;
import com.lanou.entity.User;
import com.lanou.mapper.CourseCollectMapper;
import com.lanou.mapper.CourseMapper;
import com.lanou.mapper.MyNoticeMapper;
import com.lanou.mapper.UserMapper;
import com.lanou.service.CourseCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseCollectServiceImpl
 * @Description TODO
 * @Author cuiruitong
 * @Date 2025年08月13日 11:40
 * @Version 1.0
 */
@Service
public class CourseCollectServiceImpl implements CourseCollectService {
    @Autowired
    private CourseCollectMapper courseCollectMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private MyNoticeMapper myNoticeMapper;
    @Override
    public int addCourseCollect(CourseCollect courseCollect) {
        //收藏增加积分
        User user = userMapper.selectUser(courseCollect.getUserId());
        user.setPoints(user.getPoints() + 10);
        userMapper.updateUser(user);
        //增加课程收藏数
        Course course = courseMapper.getCourseId(courseCollect.getCourseId());
        course.setCollectCount(course.getCollectCount()+1);
        courseMapper.updateCourse(course);

        MyNotice myNotice = new MyNotice(null,"课程收藏","您收藏了文章，奖励您1积分","xlzs.png",new Date(),courseCollect.getUserId());
        myNoticeMapper.addMyNotice(myNotice);
        return courseCollectMapper.addCourseCollect(courseCollect);
    }

    @Override
    public int deleteCourseCollect(CourseCollect courseCollect) {
        //取消收藏减少积分
        User user = userMapper.selectUser(courseCollect.getUserId());
        user.setPoints(user.getPoints() - 10);
        userMapper.updateUser(user);
        //减少课程收藏数
        Course course = courseMapper.getCourseId(courseCollect.getCourseId());
        course.setCollectCount(course.getCollectCount()-1);
        courseMapper.updateCourse(course);
        MyNotice myNotice = new MyNotice(null,"取消收藏","您取消了课程收藏，减少1积分","xlzs.png",new Date(),courseCollect.getUserId());
        myNoticeMapper.addMyNotice(myNotice);
        return courseCollectMapper.deleteCourseCollect(courseCollect);
    }

    @Override
    public CourseCollect getCourseCollectByCidAndUId(CourseCollect courseCollect) {
        return courseCollectMapper.getCourseCollectByCidAndUId(courseCollect);
    }

    @Override
    public PageInfo<CourseCollect> getCourseCollectByUserIdPage(Integer userId,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseCollect> list = courseCollectMapper.getCourseCollectByUserId(userId);
        PageInfo<CourseCollect> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int updateCourseCollect(CourseCollect courseCollect) {

        return courseCollectMapper.updateCourseCollect(courseCollect);
    }

    @Override
    public PageInfo<Course> getCollectCourseByUserId(Integer userId,String status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Course> list = courseCollectMapper.getCollectCourseByUserId(userId,status);
        PageInfo<Course> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
